package com.zzyl.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.entity.Reservation;
import com.zzyl.mapper.CustomerReservationMapper;
import com.zzyl.service.CustomerReservationService;

import com.zzyl.utils.UserThreadLocal;
import com.zzyl.vo.ReservationSel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class CustomerReservationServiceImpl implements CustomerReservationService {

    @Autowired
    private CustomerReservationMapper customerReservationMapper;

    /**
     * 查询取消预约的数量
     * @param userId 用户id
     * @return 取消预约的数量
     */
    @Override
    public Integer getCancelledReservationCount(Long userId) {
        int count = 0;
        List<Reservation> reservation = customerReservationMapper.getReservationById(userId);
        if (reservation == null) return 0;
        for (Reservation res : reservation) {
            if (res.getStatus() == 2) {
                count++;
            }
        }
        return count;
    }

    /**
     * 查询当天剩余预约数量
     *
     * @param userId 用户id
     * @return {
     * "time" :"  "
     * "count":"  "
     * }
     */
    // TODO -- 查询某事件预约数量  功能完成但不知其作用
    @Override
    public List<ReservationSel> getCountByTime(Long userId) {
        List<Reservation> reservations = customerReservationMapper.getReservationById(userId);
        List<String> times = customerReservationMapper.getAllReservationsByTime(userId);
        List<ReservationSel> ress = new ArrayList<>();
        if (reservations != null||ObjectUtil.isEmpty(reservations)) {
            for (Reservation res : reservations) {
                int count = 0;
                for (String time : times) {
                    if (res.getTime().equals(time)) {
                        if (ObjectUtil.isNotEmpty(res.updateTime)) {
                            if (res.updateTime.isAfter(LocalDateTimeUtil.beginOfDay(LocalDateTimeUtil.now()))&&
                                    res.updateTime.isBefore(LocalDateTimeUtil.endOfDay(LocalDateTime.now()))
                            ){
                                count++;
                            }
                        }
                        else {
                            if (res.createTime.isAfter(LocalDateTimeUtil.beginOfDay(LocalDateTimeUtil.now()))&&
                                    res.createTime.isBefore(LocalDateTimeUtil.endOfDay(LocalDateTime.now()))
                            ){
                                count++;
                            }
                        }
                        ress.add(new ReservationSel( count,time));
                    }
                }

            }
        }
        return ress;
    }

    @Override
    public void addReservation(Reservation reservation) {
        reservation.setCreateBy(UserThreadLocal.getUserId());
        reservation.setStatus(0);
        customerReservationMapper.insertReservation(reservation);
    }

    @Override
    public PageResponse<Reservation> queryReservationByPage(Integer pageNum, Integer pageSize, Integer status) {
        PageHelper.startPage(pageNum,pageSize);
        List<Reservation> list =  customerReservationMapper.queryReservationByPage(status,UserThreadLocal.getUserId());
        Page<Reservation> pages = (Page<Reservation>) list;
        return PageResponse.of(pages, Reservation.class);
    }

    @Override
    public void delReservationStatus(Long id) {
        customerReservationMapper.delReservationStatus(id);
    }


}
